package Action;

import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import Bean.NewsBean;
import Utils.Common;
import Utils.StringUtil;
import Utils.XmlReplace;

public class NewsFromArt{
	public String getAllNews(Connection conn){
		int list[]={0,27,24};
		String r="";
		for(int i=0; i < list.length; i++){
			r += ""+getNewsCat(list[i], conn)+"_";
		}
		return Common.makeSuccessXML("<status value='"+r+"'/>");
	}
	
	public int getNewsCat(int catid, Connection conn){
		String url = "http://art.rolo.vn/a/api?act=listForTopNews&catid="+catid+"&n="+10;
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		PreparedStatement stmt = null;
		try{
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(url);
			Element elem = doc.getDocumentElement();
			NodeList nl = elem.getElementsByTagName("news");
			if(catid==0) catid=7;
			else if(catid==27) catid=3;
			else if(catid==24) catid = 8;
			int total  = 0 ;
			for (int n = 0; n < nl.getLength(); n++) {
				Element news = (Element) nl.item(n);
				String title = news.getAttribute("title");
				String date = news.getAttribute("createddate");
//				String img = news.getAttribute("thumbnail");
				String summary = news.getAttribute("description");
				String newsId = news.getAttribute("newsid");
				
				String info = getContent(newsId);
				if(!info.equals("")){
					String content = info.split("@VTC@")[0];
					String source = info.split("@VTC@")[1];
					String link = info.split("@VTC@")[2];
					String keyword = info.split("@VTC@")[3];
					NewsBean newsbean = new NewsBean();
					newsbean.setNewsId(newsId);
					newsbean.setTitle(title);
					newsbean.setSummary(summary);
					newsbean.setContent(content);
					newsbean.setPicture("");
					newsbean.setThumbnail("");
					newsbean.setHadImg(1);
					newsbean.setLink(link);
					newsbean.setSource(source);
					String newdate = StringUtil.convertDate(date,"vn").replace(",", "");
					newdate = newdate.split(" ")[1]+" "+newdate.split(" ")[0];
					newsbean.setDatePublished(newdate);
					newsbean.setTempCat(catid);
					newsbean.setKeyword(keyword);
					newsbean.setType(1);
					newsbean.setPoster("auto");
					newsbean.setStatus(0);
					newsbean.setTotalNews(1);
					if(new News().insertNews(newsbean,conn)){
						if(new News().insertCat(""+catid, newsId, conn)){
							total++;
							if(total==5) break;
						}
					}
				}
				
			}
			return total;
		}catch (Exception e) {
			return -1;
		}finally{
			if(stmt!=null) try{stmt.close();}catch (Exception e) {}
		}
	}
	
	public String getContent(String newsId){
		String url = "http://art.rolo.vn/a/api?act=viewContent&newsid="+newsId;
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		PreparedStatement stmt = null;
		try{
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(url);
			Element elem = doc.getDocumentElement();
			NodeList nl = elem.getElementsByTagName("news");
			for (int n = 0; n < nl.getLength(); n++) {
				return XmlReplace.unescape(((Element)nl.item(n)).getAttribute("content"))+"@VTC@"+
					   XmlReplace.unescape(((Element)nl.item(n)).getAttribute("source"))+"@VTC@"+
					   XmlReplace.unescape(((Element)nl.item(n)).getAttribute("orglink"))+"@VTC@"+
					   XmlReplace.unescape(((Element)nl.item(n)).getAttribute("keyword"))+"@VTC@"  ;
			}
			return "";
		}catch (Exception e) {
			return "";
		}finally{
			if(stmt!=null) try{stmt.close();}catch (Exception e) {}
		}
	}
}